package org.mics.token.utils;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;

/**
 * 时间工具类
 * @author mics
 * @date 2022-8-19
 * @version  1.0
 */
public class DateTimeUtil {

    /**
     * 获取第二天的凌晨三点 - 主要用于JWT过期时间
     * @author mics
     * @date 2022-8-19
     * @version  1.0
     * @return 二天的凌晨三点
     */
    public static Date getThreeClockAmOfTheNextDay() {
        LocalDateTime currentLocalDateTime = LocalDateTime.now();
        LocalDateTime nextDay = currentLocalDateTime.plusDays(1);
        LocalDateTime threeClockAmOfTheNextDay = LocalDateTime.of(nextDay.getYear(), nextDay.getMonth(), nextDay.getDayOfMonth(), 3, 0, 0);
        return convertLocalDateTimeToDate(threeClockAmOfTheNextDay);
    }

    /**
     * 获取第七天的凌晨三点 - 主要用于JWT过期时间
     * @author mics
     * @date 2022-8-19
     * @version  1.0
     * @return 第七天的凌晨三点 
     */
    public static Date getThreeClockAmOfSeventhDay() {
        LocalDateTime currentLocalDateTime = LocalDateTime.now();
        LocalDateTime nextDay = currentLocalDateTime.plusDays(7);
        LocalDateTime threeClockAmOfSeventhDay = LocalDateTime.of(nextDay.getYear(), nextDay.getMonth(), nextDay.getDayOfMonth(), 3, 0, 0);
        return convertLocalDateTimeToDate(threeClockAmOfSeventhDay);
    }

    /**
     * 转换LocalDateTime为Date对象
     * @author mics
     * @date 2022-8-19
     * @version  1.0
     * @param localDateTime   localDateTime类型的时间
     * @return  date型时间
     */
    public static Date convertLocalDateTimeToDate(LocalDateTime localDateTime) {
        ZoneId zoneId = ZoneId.systemDefault();
        ZonedDateTime zonedDateTime = localDateTime.atZone(zoneId);
        return Date.from(zonedDateTime.toInstant());
    }
}
